﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DisplayBoardInfo.DataSource;
using System.Windows.Markup;

namespace DisplayBoardInfo.Queue
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    /// 
    public partial class Queue : UserControl
    {
        public Queue()
        {
            InitializeComponent();
        }

        private bool _anonymous;

        public bool Anonymous
        {
            get { return _anonymous; }
            set { _anonymous = value; RefreshData(); }
        }


        private DataSource.DataSource _provider;
        public Object MyDataContent
        {
            get
            {
                return _provider;
            }
            set
            {
                DataSource.DataSource provider = value as DataSource.DataSource;
                _provider = provider;
                RefreshData();
            }
        }

        private void RefreshData()
        {
            if (_provider == null)
                return;
            DataSourceObject data = _provider.FetchData();
            PhysicianName.Text = data.Physician.Name;
            PhysicianSpeciality.Text = data.Physician.Speciality;
            ClearBoxes();
            CreateCurrentVisit(data.Visits[0]);
            CreateOtherVisits(data.Visits);
        }

        private void ClearBoxes()
        {
            Patient1_Name.Text = "";
            Patient1_In.Text = "";
            Patient1_Out.Text = "";

            Patient2_Name.Text = "";
            Patient2_In.Text = "";
            Patient2_Out.Text = "";

            Patient3_Name.Text = "";
            Patient3_In.Text = "";
            Patient3_Out.Text = "";

            Patient4_Name.Text = "";
            Patient4_In.Text = "";
            Patient4_Out.Text = "";
        }

        private void CreateOtherVisits(List<PatientVisit> list)
        {
            if (list.Count < 2)
                return;
            Patient2_Name.Text = GetName(Anonymous, list[1]);
            Patient2_In.Text = list[1].VisitBegin;
            Patient2_Out.Text = list[1].VisitEnd;

            if (list.Count < 3)
                return;
            Patient3_Name.Text = GetName(Anonymous, list[2]);
            Patient3_In.Text = list[2].VisitBegin;
            Patient3_Out.Text = list[2].VisitEnd;

            if (list.Count < 4)
                return;
            Patient4_Name.Text = GetName(Anonymous, list[3]);
            Patient4_In.Text = list[3].VisitBegin;
            Patient4_Out.Text = list[3].VisitEnd;
        }

        private void CreateCurrentVisit(PatientVisit patientVisit)
        {
            if (patientVisit == null)
                return;
            Patient1_Name.Text = GetName(Anonymous, patientVisit);
            Patient1_In.Text = patientVisit.VisitBegin;
            Patient1_Out.Text = patientVisit.VisitEnd;
        }

        private String GetName(bool anonymous, PatientVisit visit)
        {
            if (anonymous)
            {
                return visit.Name.Substring(0, visit.Name.IndexOf(' ')) + " " + visit.BirthDate;
            }
            else
            {
                return visit.Name;
            }
        }
    }
}
